# MediaMentionsFigure.R
# Goal: Create chart of media mentions
# B.Lee
# Date Created: 2024-06-25
# Last Updated: 2024-06-26 \ bal

# set up directories

# uncomment the line below and add working directory path between the quotation marks
# master_directory = "ADD WORKING DIRECTORY HERE/"

data_folder = paste0(master_directory,"Data/")
processing_data_folder = paste0(data_folder, "Processing")
do_folder = paste0(master_directory,"Do/")
results_folder = paste0(master_directory,"Results/")
charts_folder = paste0(master_directory,"Charts/")

# load libraries 
library(tidyverse)
library(readxl)
library(ggplot2)
library(ggthemes)

# references to arp act articles
arp <- read_xlsx(paste0(data_folder,"Raw/NewsArticleCounts/NewsArticleCountsV2.xlsx"), sheet = "American Rescue Plan")[1:24,1:9]

arp1 <- arp %>%
  summarize(arp_ttl = sum(`American Rescue Plan`),
            arp_sen_ttl = sum(`American Resuce Plan AND Senator`),
            arp_gov_ttl = sum(`American Rescue Plan AND Governor`),
            arp_gov_invest_ttl = sum(`American Rescue Plan AND Governor AND invest`),
            arp_sen_invest_ttl = sum(`American Rescue Plan AND Senator AND invest`),
            arp_gov_spend_ttl = sum(`American Rescue Plan AND Governor AND spend`),
            arp_sen_spend_ttl = sum(`American Rescue Plan AND Senator AND spend`)) %>%
  mutate(ratio_gov_to_sen = arp_gov_ttl/arp_sen_ttl,
         act = "Ratio of ARP Act Articles Referencing Governors Vs. Senators") %>%
  select(ratio_gov_to_sen, act)

# references to cares act articles
cares <-read_xlsx(paste0(data_folder,"Raw/NewsArticleCounts/NewsArticleCountsV2.xlsx"), sheet = "Cares Act")[1:12,1:9]

cares1 <- cares %>%
  summarize(cares_ttl = sum(`CARES Act`),
            cares_sen_ttl = sum(`\"Cares Act\" AND Senator`),
            cares_gov_ttl = sum(`\"Cares Act\" AND Governor`),
            cares_gov_invest = sum(`\"Cares Act\" AND Governor AND invest`),
            cares_gov_spend = sum(`"Cares Act\" AND Governor AND spend`), 
            cares_sen_invest = sum(`\"Cares Act\" AND Senator AND invest`),
            cares_sen_spend = sum(`\"Cares Act\" AND Senator AND spend`)) %>%
  mutate(ratio_gov_to_sen = cares_gov_ttl/cares_sen_ttl,
         act = "Ratio of CARES Act Articles Referencing Governors Vs. Senators") %>%
  select(ratio_gov_to_sen, act)

# merge dfs together
news_data <- rbind(arp1, cares1)

# wrap the acts label (it's too long for the chart)
news_data1 <- news_data %>%
  mutate(act = str_wrap(act, width = 30))

# start pdf
pdf(file = paste0(charts_folder,"news_ratio_plot_20240626.pdf"),   # The directory you want to save the file in
    width = 15.625, # The width of the plot in inches
    height = 12.5) # The height of the plot in inches

# plot data    
ggplot(news_data1, aes(x = factor(act, levels = c("Ratio of CARES Act Articles\nReferencing Governors Vs.\nSenators", "Ratio of ARP Act Articles\nReferencing Governors Vs.\nSenators")), 
                      y = ratio_gov_to_sen)) +
  geom_col(width = 0.45, 
           fill = "#156082") +
  scale_y_continuous(breaks = seq(0, 7, by = 1),
                     limits = c(0,7)) +

  labs(x = "",
       y = "") + 
  theme_stata(scheme = "s1mono")  +
  theme(text = element_text(size = 30),
        axis.title.y = element_text(vjust = 1.5),
        axis.text.x = element_text(vjust = -1))

# end pdf
dev.off()

# save plot
# ggsave(paste0(charts_folder,"news_ratio_plot_20240625.png"), news_plot, width= 1500, height=1200, units = "px")

  

